package com.lwb.ztx.user.widget;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.database.greendao.db.Collect;
import com.lwb.ztx.user.presenter.CollectPresenter;
import com.lwb.ztx.user.presenter.CollectPresenterImpl;
import com.lwb.ztx.user.view.CollectView;
import com.lwb.ztx.utils.ActivitySkipUtils;
import com.lwb.ztx.utils.ListUtils;
import com.lwb.ztx.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class CollectActivity extends SwipeBackActivity implements CollectView, CollectAdapter
        .OnItemClickListener,CollectAdapter.OnItemSelectListener, View.OnClickListener {
    @InjectView(R.id.back)
    View back;
    @InjectView(R.id.title)
    public TextView title;
    @InjectView(R.id.collect_recycler_view)
    RecyclerView recyclerView;
    CollectAdapter adapter;
    private CollectPresenter presenter;
    @InjectView(R.id.collect_null)
    View collectNullView;
    @InjectView(R.id.left_btn)
    TextView leftBtn;
    @InjectView(R.id.collect_news_item_select_all)
    TextView selectAll;
    @InjectView(R.id.collect_news_item_collect_delete)
    TextView delete;
    @InjectView(R.id.collect_bottom_view)
    View bottomView;
    private List<Collect> deleteCollects = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new CollectPresenterImpl(this);
        setContentView(R.layout.activity_collect);
        ButterKnife.inject(this);
        title.setText("我的收藏");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CollectAdapter(this, this, this);
        recyclerView.setAdapter(adapter);
        presenter.getCollects(this);
//        leftBtn.setVisibility(View.VISIBLE);
        leftBtn.setOnClickListener(this);
        selectAll.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @OnClick(R.id.back)
    public void onBackClick(View view) {
        this.finish();
    }

    @Override
    public void setCollectNews(List<Collect> collectNews) {
        if (ListUtils.getSize(collectNews) == 0) {
            collectNullView.setVisibility(View.VISIBLE);
        } else {
            collectNullView.setVisibility(View.GONE);
            adapter.setCollects(collectNews);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClick(NewsBean bean) {
        ActivitySkipUtils.readNews(this, bean);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getCollects(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                adapter.setEdit(!adapter.isEdit());
                adapter.notifyDataSetChanged();
                if (adapter.isEdit()) {
                    leftBtn.setText("完成");
                    bottomView.setVisibility(View.VISIBLE);
                } else {
                    leftBtn.setText("编辑");
                    bottomView.setVisibility(View.GONE);
                }
                break;
            case R.id.collect_news_item_select_all:
                break;
            case R.id.collect_news_item_collect_delete:
                presenter.deleteCollects(this,deleteCollects);
                deleteCollects.clear();
                break;
        }
    }

    @Override
    public void OnItemChecked(Collect collect) {
        LogUtils.i("add " + collect.toString());
        deleteCollects.add(collect);
    }

    @Override
    public void OnItemUnChecked(Collect collect) {
        LogUtils.i("del " + collect.toString());
        deleteCollects.remove(collect);
    }
}
